package com.ruoyi.project.relation.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.relation.domain.BusinessRelation;
import com.ruoyi.project.relation.domain.builder.BusinessRelationQOBuilder;
import com.ruoyi.project.relation.domain.dto.BusinessRelationDO;
import com.ruoyi.project.relation.domain.qo.BusinessRelationQO;
import com.ruoyi.project.wb.utils.TokenUserUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.shoukaiseki.aqoeous.config.Validator;
import org.shoukaiseki.common.exception.MessageVirtualException;
import org.shoukaiseki.common.logger.Logger;
import org.shoukaiseki.common.logger.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.relation.mapper.BusinessRelationMapper;
import com.ruoyi.project.relation.service.IBusinessRelationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BeanPropertyBindingResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 物料清单关联Service业务层处理
 *
 * @author wb
 * @date 2021-03-28
 */
@Service
public class BusinessRelationServiceImpl implements IBusinessRelationService
{
    public static final Logger logger = LoggerFactory.getLogger(BusinessRelationServiceImpl.class);

    @Autowired
    private BusinessRelationMapper businessRelationMapper;

    @Resource
    private Validator validator;


    /**
     * 精确查询物料清单关联列表,用于逻辑关联表处理
     *
     * @param businessRelationQO 物料清单关联
     * @return 物料清单关联
     */
    @Override
    public List<BusinessRelation> exactQueryBusinessRelationList(BusinessRelationQO businessRelationQO)
    {
        List<BusinessRelation> list=businessRelationMapper.exactQueryBusinessRelationList(businessRelationQO);
        return list;
    }

    /**
     * 增加多个进行互相关联
     * @param businessRelationDOs
     */
    @Transactional
    @Override
    public void addMultipleInterrelate(BusinessRelationDO... businessRelationDOs) {
        SysUser sysUser= TokenUserUtils.getUser();
        Date nowDate = DateUtils.getNowDate();

        for (int i = 0; i < businessRelationDOs.length; i++) {
            BusinessRelationDO sourceBusinessRelationDO = businessRelationDOs[i];
            for (int j = i+1; j < businessRelationDOs.length; j++) {
                BusinessRelationDO targetBusinessRelationDO = businessRelationDOs[j];

                validator.validate(sourceBusinessRelationDO,new BeanPropertyBindingResult(sourceBusinessRelationDO,"sourceBusinessRelationDO" ),true);
                validator.validate(targetBusinessRelationDO,new BeanPropertyBindingResult(targetBusinessRelationDO,"targetBusinessRelationDO" ),true);

                addInterrelate(sourceBusinessRelationDO,targetBusinessRelationDO,sysUser,nowDate);
                //源,目标互换后再添加一条记录
                addInterrelate(targetBusinessRelationDO,sourceBusinessRelationDO,sysUser,nowDate);
            }
        }
    }

    @Override
    public void addInterrelate(BusinessRelationDO sourceBusinessRelationDO, BusinessRelationDO targetBusinessRelationDO) {
        Date nowDate = DateUtils.getNowDate();
        SysUser user = TokenUserUtils.getUser();
        addInterrelate(sourceBusinessRelationDO,targetBusinessRelationDO,user,nowDate);
    }

    /**
     * 增加关联
     * @param sourceBusinessRelationDO
     * @param targetBusinessRelationDO
     * @param sysUser
     * @param nowDate
     */
    @Transactional
    @Override
    public void addInterrelate(BusinessRelationDO sourceBusinessRelationDO,BusinessRelationDO targetBusinessRelationDO,SysUser sysUser,Date nowDate){
        BusinessRelationQO businessRelationSO = new BusinessRelationQO();
        businessRelationSO.setSourceOwnerId(sourceBusinessRelationDO.getOwnerId());
        businessRelationSO.setSourceOwnerName(sourceBusinessRelationDO.getOwnerName());
        businessRelationSO.setSourceOwnerType(sourceBusinessRelationDO.getOwnerType());
        businessRelationSO.setTargetOwnerId(targetBusinessRelationDO.getOwnerId());
        businessRelationSO.setTargetOwnerName(targetBusinessRelationDO.getOwnerName());
        businessRelationSO.setTargetOwnerType(targetBusinessRelationDO.getOwnerType());
        businessRelationSO.setDelFlag(null);
        List<BusinessRelation> businessRelationList = businessRelationMapper.exactQueryBusinessRelationList(businessRelationSO);
        BusinessRelation businessRelationTmp = businessRelationList.stream().findFirst().orElse(null);
        if (businessRelationTmp!=null) {
            //删除标志
            if (businessRelationTmp.getDelFlag().intValue()==1) {
                businessRelationTmp.setDelFlag(0);
                BaseEntity.setUpdate(businessRelationTmp,sysUser,nowDate);
                businessRelationMapper.updateBusinessRelation(businessRelationTmp);
            }
            return;
        }
        businessRelationTmp=new BusinessRelation();
        BeanUtils.copyProperties(businessRelationSO,businessRelationTmp);
        BaseEntity.setCreate(businessRelationTmp,sysUser,nowDate);
        businessRelationMapper.insertBusinessRelation(businessRelationTmp);
    }

    /**
     * 删除关联
     * @param businessRelationDO
     */
    @Transactional
    @Override
    public void deleteBusinessRelationByBusinessRelationDO(BusinessRelationDO businessRelationDO){
        BusinessRelationQO businessRelationQO = BusinessRelationQOBuilder.newBusinessRelationQO()
                .sourceOwner(businessRelationDO).build();
        businessRelationMapper.deleteBusinessRelationByBusinessRelationQO(businessRelationQO);
        businessRelationQO = BusinessRelationQOBuilder.newBusinessRelationQO()
                .targetOwner(businessRelationDO).build();
        businessRelationMapper.deleteBusinessRelationByBusinessRelationQO(businessRelationQO);
    }

}
